<?php
use Doctrine\DBAL\Cache\ResultCacheStatement;
class Admin_ProvidersController extends Model3_Controller
{
    private $_credentials;
   // protected $_config;
    
    public function init()
    {
        if(!Model3_Auth::isAuth())
            $this->redirect();
        else
        {
            $role = Model3_Auth::getCredentials('user_type');
            if( $role !== DefaultDb_Entity_User::TYPE_ADMIN )
            {
               Model3_Auth::deleteCredentials();
               $this->redirect();
            }
        }
        $this->_credentials = Model3_Auth::getCredentials();
        $this->view->setTemplate('Admin');
    }
    
    public function indexAction()
    {

    }
    
    public function getListProviderAction(){/*-----------FUNCION PARA OBTENER EL LISTADO DE PROVEEDORES----------------*/
    	
    	$this->view->setUseTemplate(FALSE);
    	header("Content-Type: application/json");
    
       	$em = $this->getEntityManager('DefaultDb');
       	$lista = $em->getRepository('DefaultDb_Entity_Provider')->findAll();
        
    	$arrJson = array();
    	foreach($lista as $item)
    	{
    		$temp = array(
    				"idProvider" => $item->getId(),
    				"company" => $item->getCompanie(),
    				"fecha" => $item->getDate(),
    				"empresa" => $item->getBusinessName(),
    				"alias" => $item->getAlias(),
    				"rfc" => $item->getRfc(),
    				"director" => $item->getNameManager(),
    				"mail" => $item->getEmail(),    
    				"address" => $item->getStreet()." ".$item->getNum().", ".$item->getSuburb()." ".$item->getZipCode()
    		);
    		array_push($arrJson, $temp);
    	}
    	
    	$this->view->result = json_encode($arrJson);
    }    

 	public function  frmProviderAction(){/*-----------FUNCION PARA LLAMAR A LA VISTA QUE GUARDA UN NUEVO PROVEEDOR----------------*/

 		$em = $this->getEntityManager('DefaultDb');
 		$lista = $em->getRepository('DefaultDb_Entity_State')->findAll();
 		
 		$combo ="";
 		foreach($lista as $item)
 		{
 			$combo .= "<option value='".$item->getId();
 			$combo .= "'>".$item->getName()."</option>\n";
 		}
 		$this->view->states = $combo;
 			
 	}   
    
    public function saveProviderAction(){/*-----------FUNCION PARA GUARDA O MODIFICAR DATOS DE UN PROVEEDOR----------------*/

    	$this->view->setUseTemplate(FALSE);
    	$post = $this->getRequest()->getPost();
		$request = explode("!", $post['form']);
		$form = explode("*",$request[0]);
    	$action = $request[1];
    	$idProvider = $request[2];

    	 $em = $this->getEntityManager('DefaultDb');
    	
    	$user = $em->getRepository('DefaultDb_Entity_User')->findOneBy(array('id_users' => $this->_credentials));
    	
    	if ($action == "btnGrabarProveedor")
    	{
    		$provider = new DefaultDb_Entity_Provider();
			$provider->setDate(date('Y-m-d H:m:s'));
    	}
    	if ($action == "btnEditarProveedor")
    	{
	  		$provider = ($idProvider) ? $em->getRepository('DefaultDb_Entity_Provider')->findOneBy(array('id_providers' => $idProvider)) : new DefaultDb_Entity_Provider();
    	}
    	
    	$em->getConnection()->beginTransaction();
    	try
    	{
    		$provider->setBusinessName($form[0]);
    		$provider->setRfc($form[1]);
    		$provider->setNameManager($form[2]);
    		$provider->setStreet($form[4]);
    		$provider->setNum($form[5]);
    		$provider->setSuburb($form[6]);
    		$provider->setZipCode($form[7]);
    		$provider->setEmail($form[8]);
    		$provider->setAlias($form[9]);
    		$provider->setCity($form[10]);
    		$provider->setState($form[11]);
    		$provider->setStatus(1);
    		$provider->setCompanie($user->getId());

    		$em->persist($provider);
    		$em->flush();
    		
    		
    	  	$em->getConnection()->commit();
    		$result = "Datos Guardados";
    	} 
    	catch(Exception $e)
    	{
    		$em->getConnection()->rollback();
    		$em->close();
    		$result = "Error al Guardar los Datos";
    	}
    	
    	$this->view->result = $result;
    }

    public function delProviderAction()/*-----------FUNCION PARA CAMBIAR EL ESTADO DE UN PROVEEDOR----------------*/
    {
    	$this->view->setUseTemplate(FALSE);
    	$post= $this->getRequest()->getPost();
    	$em = $this->getEntityManager('DefaultDb');
    	$provider = ($post['idprovider']) ? $em->getRepository('DefaultDb_Entity_Provider')->findOneBy(array('id_providers' => $post['idprovider'])) : new DefaultDb_Entity_Provider();
    	$provider->setAlias('Alias cambiado');
    	$provider->setEmail('Mail cambiado');
        $em->persist($provider);
        $em->flush();
    }
    
    public  function  editProviderAction()/*-----------FUNCION PARA LLAMAR A LA VISTA QUE MODIFICA LOS DATOS DE UN PROVEEDOR----------------*/
    {
    	if( !is_null($this->getRequest()->getParam('idprovider')) && is_numeric($this->getRequest()->getParam('idprovider')) )
    	{
    		$idProvider = $this->getRequest()->getParam('idprovider');
    		$em = $this->getEntityManager('DefaultDb');
    		$cons = $em->getRepository('DefaultDb_Entity_Provider')->findOneBy(array('id_providers' => $idProvider));
    		
    		$this->view->idprovider = $idProvider;
    		$this->view->businessName = $cons->getbusinessName();
    		$this->view->rfc = $cons->getRfc();
    		$this->view->namemanager = $cons->getnameManager();
    		$this->view->date = date("Y-m-d", strtotime($cons->getDate()));
    		$this->view->street = $cons->getStreet();
    		$this->view->num = $cons->getNum();
    		$this->view->suburb = $cons->getSuburb();
    		$this->view->zipcode = $cons->getZipCode();
    		$this->view->email = $cons->getEmail();
    		$this->view->alias = $cons->getAlias();
    		$this->view->city = $cons->getCity();
    		$this->view->state = $cons->getState();
    	
    	}
    	$lista = $em->getRepository('DefaultDb_Entity_State')->findAll();
    	 
    	$combo ="";
    	foreach($lista as $item)
    	{
    		$combo .= "<option value='".$item->getId();
    		$combo .= "'>".$item->getName()."</option>\n";
    	}
    	$this->view->states = $combo;
    }
    
}










